/*
 * This file is part of quickLoad, a software for gamers
 *
 * Copyright (C) 2005-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using ListViewEmbeddedControls;

namespace QuickLoad.UI
{
	/// <summary>
	/// Zusammenfassung fr FormBrowseZip.
	/// </summary>
	public class FormBrowseZip : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.RadioButton radioButton2;
		private FormMain Vater;
		private string zipfile = string.Empty;
		private string zippath = string.Empty;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ImageList Icons;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ListView listView2;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.ColumnHeader columnHeader6;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.ColumnHeader columnHeader7;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader8;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.ComponentModel.IContainer components;

		public FormBrowseZip(FormMain parent, string zipfile)
		{
			//
			// Erforderlich fr die Windows Form-Designeruntersttzung
			//
			InitializeComponent();
			Vater = parent;
			this.zipfile = zipfile;
			//FileList = parent.FileList;
			foreach (Image img in Vater.Icons.Images)
			{
				this.Icons.Images.Add(img);
			}
			//
			// TODO: Fgen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu
			//
		}

		/// <summary>
		/// Die verwendeten Ressourcen bereinigen.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Vom Windows Form-Designer generierter Code
		/// <summary>
		/// Erforderliche Methode fr die Designeruntersttzung. 
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor gendert werden.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormBrowseZip));
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.Icons = new System.Windows.Forms.ImageList(this.components);
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.listView2 = new System.Windows.Forms.ListView();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.panel1 = new System.Windows.Forms.Panel();
			this.button2 = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader3,
																						this.columnHeader7,
																						this.columnHeader2});
			this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.listView1.Enabled = false;
			this.listView1.FullRowSelect = true;
			this.listView1.Location = new System.Drawing.Point(3, 16);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(602, 151);
			this.listView1.SmallImageList = this.Icons;
			this.listView1.TabIndex = 1;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Name";
			this.columnHeader1.Width = 195;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "Type";
			this.columnHeader3.Width = 170;
			// 
			// columnHeader7
			// 
			this.columnHeader7.Text = "Size";
			this.columnHeader7.Width = 84;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "Full Name";
			this.columnHeader2.Width = 148;
			// 
			// Icons
			// 
			this.Icons.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.Icons.ImageSize = new System.Drawing.Size(16, 16);
			this.Icons.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.listView1);
			this.groupBox1.Controls.Add(this.groupBox2);
			this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.groupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox1.Location = new System.Drawing.Point(0, 48);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(608, 370);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Available Files (double-click to add)";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.listView2);
			this.groupBox2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.groupBox2.Location = new System.Drawing.Point(3, 167);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(602, 200);
			this.groupBox2.TabIndex = 5;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Selected Files (double-click to remove)";
			// 
			// listView2
			// 
			this.listView2.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader4,
																						this.columnHeader5,
																						this.columnHeader8,
																						this.columnHeader6});
			this.listView2.Dock = System.Windows.Forms.DockStyle.Fill;
			this.listView2.Enabled = false;
			this.listView2.FullRowSelect = true;
			this.listView2.Location = new System.Drawing.Point(3, 16);
			this.listView2.Name = "listView2";
			this.listView2.Size = new System.Drawing.Size(596, 181);
			this.listView2.SmallImageList = this.Icons;
			this.listView2.TabIndex = 2;
			this.listView2.View = System.Windows.Forms.View.Details;
			this.listView2.DoubleClick += new System.EventHandler(this.listView2_DoubleClick);
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "Name";
			this.columnHeader4.Width = 194;
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "Type";
			this.columnHeader5.Width = 169;
			// 
			// columnHeader8
			// 
			this.columnHeader8.Text = "Size";
			this.columnHeader8.Width = 83;
			// 
			// columnHeader6
			// 
			this.columnHeader6.Text = "Full Name";
			this.columnHeader6.Width = 118;
			// 
			// radioButton1
			// 
			this.radioButton1.Checked = true;
			this.radioButton1.Dock = System.Windows.Forms.DockStyle.Top;
			this.radioButton1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.radioButton1.Location = new System.Drawing.Point(0, 0);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new System.Drawing.Size(608, 24);
			this.radioButton1.TabIndex = 0;
			this.radioButton1.TabStop = true;
			this.radioButton1.Text = "Add archive as whole";
			// 
			// radioButton2
			// 
			this.radioButton2.Dock = System.Windows.Forms.DockStyle.Top;
			this.radioButton2.Enabled = false;
			this.radioButton2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.radioButton2.Location = new System.Drawing.Point(0, 24);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(608, 24);
			this.radioButton2.TabIndex = 1;
			this.radioButton2.Text = "Add specific zip-content, each file is uploaded seperately (select files below):";
			this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.button2);
			this.panel1.Controls.Add(this.button1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel1.Location = new System.Drawing.Point(0, 418);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(608, 24);
			this.panel1.TabIndex = 3;
			// 
			// button2
			// 
			this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button2.Location = new System.Drawing.Point(440, 0);
			this.button2.Name = "button2";
			this.button2.TabIndex = 1;
			this.button2.Text = "Cancel";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button1
			// 
			this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button1.Location = new System.Drawing.Point(520, 0);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "OK";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(560, 0);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(48, 48);
			this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox1.TabIndex = 4;
			this.pictureBox1.TabStop = false;
			// 
			// FormBrowseZip
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(608, 442);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.radioButton2);
			this.Controls.Add(this.radioButton1);
			this.Controls.Add(this.panel1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "FormBrowseZip";
			this.Text = "Select the files you want to add";
			this.Load += new System.EventHandler(this.FormBrowseZip_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void FormBrowseZip_Load(object sender, System.EventArgs e)
		{
			string pathToZip = zipfile;
			this.Cursor = Cursors.WaitCursor;
			listView1.BeginUpdate();
			ZipInputStream s = new ZipInputStream(File.OpenRead(zipfile));					
			ZipEntry theEntry = null;
			string extension = "";
			int count = 0;

			try
			{
				theEntry = s.GetNextEntry();
			}
			catch(ZipException z1)
			{				
				MessageBox.Show("Unable to work with:\r\r" + pathToZip + "\r\r" + z1.Message.ToString());
				//statusBar1.Panels[0].Text = "";
				return;
			}

			listView1.Items.Clear();
			// we use the statusbar all the time to get the current zipfile location
			while (theEntry != null) 
			{								
				ListViewItem fileItem = new ListViewItem();
				string directoryName = Path.GetDirectoryName(theEntry.Name);
				string fileName = Path.GetFileName(theEntry.Name);
				if (fileName != "")
				{
					extension = Path.GetExtension(fileName);
					//if (extension != "")
						//extension = extension.Substring(1).ToUpper();
				}
				fileItem.ImageIndex = Vater.getItemIndex(extension);
				//setIcons(extension, ref fileItem, ref fileType);
				directoryName = directoryName.Replace("/", "\\");
				if (!directoryName.EndsWith("\\") && directoryName != "")
					directoryName = directoryName + "\\";
				
				if (fileName == "")
				{
					try
					{
						theEntry = s.GetNextEntry();
					}
					catch(ZipException z1)
					{
						MessageBox.Show("Unable to work with:\r\r" + pathToZip + "\r\r" + z1.Message.ToString());
						//statusBar1.Panels[0].Text = "";
						break;
					}
					continue;
				}
				string ext = Path.GetExtension(fileName).ToLower();			
				if (ext == ".zip" || ext == ".bmp" || ext == ".jpg" || ext == ".dem")
				{
					fileItem.Text = Path.GetFileNameWithoutExtension(fileName);
					if (theEntry.CompressionMethod == CompressionMethod.Stored)
						fileItem.ForeColor = Color.DarkGreen;
					fileItem.UseItemStyleForSubItems = false;
					fileItem.SubItems.Add("Zipped " + Vater.getType(extension));
					fileItem.SubItems.Add(Vater.ConvertBytes(theEntry.Size));
					fileItem.SubItems.Add(theEntry.Name);
					listView1.Items.Insert(count++, fileItem);
				}
				try
				{
					theEntry = s.GetNextEntry();
				}
				catch(ZipException z1)
				{
					MessageBox.Show("Unable to work with:\r\r" + pathToZip + "\r\r" + z1.Message.ToString());
					//statusBar1.Panels[0].Text = "";
					break;
				}
			}
			//statusBar1.Panels[1].Text = count.ToString() + " Files";
			s.Close();
			//executeSort(currentColumn);
			listView1.EndUpdate();
			this.Cursor = Cursors.Default;
		}

		private void listView1_DoubleClick(object sender, System.EventArgs e)
		{
			ListViewItem item = listView1.GetItemAt(listView1.PointToClient(Control.MousePosition).X,listView1.PointToClient(Control.MousePosition).Y);
			if (item == null)
			{
				return;
			}
			listView1.Items.Remove(item);
			listView2.Items.Add(item);
		}

		private void listView2_DoubleClick(object sender, System.EventArgs e)
		{
			ListViewItem item = listView2.GetItemAt(listView2.PointToClient(Control.MousePosition).X,listView2.PointToClient(Control.MousePosition).Y);
			if (item == null)
			{
				return;
			}
			listView2.Items.Remove(item);
			listView1.Items.Add(item);
		}

		private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
		{
			if (radioButton2.Checked)
			{
				listView1.Enabled=true;
				listView2.Enabled=true;
			}
			else
			{
				listView1.Enabled=false;
				listView2.Enabled=false;
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			if (!Vater.uploading)
			{
				
				if (radioButton1.Checked)
				{
					FileInfo Datei = new FileInfo(zipfile);
					XPTable.Models.Row row = new XPTable.Models.Row();
					row.Cells.Add(new XPTable.Models.Cell(Path.GetFileNameWithoutExtension(zipfile)));
					row.Cells[0].Data = zipfile;
					row.Cells.Add(new XPTable.Models.Cell(Vater.getType(Datei.Extension),Vater.Icons.Images[Vater.getItemIndex(Path.GetExtension(zipfile))]));
					row.Cells[1].Data = String.Empty;
					
					row.Cells.Add(new XPTable.Models.Cell(Vater.ConvertBytes(Datei.Length)));
					row.Cells.Add(new XPTable.Models.Cell("Queued"));
					row.Cells.Add(new XPTable.Models.Cell(0));
					row.Cells.Add(new XPTable.Models.Cell(""));
					row.Cells.Add(new XPTable.Models.Cell(""));
					Vater.AddRow(row);
				}
				else
				{
					foreach (ListViewItem item in listView2.Items)
					{
						XPTable.Models.Row row = new XPTable.Models.Row();
						row.Cells.Add(new XPTable.Models.Cell(Path.GetFileNameWithoutExtension(item.SubItems[0].Text)));
						row.Cells[0].Data = item.SubItems[3].Text;
						row.Cells.Add(new XPTable.Models.Cell(item.SubItems[1].Text,Vater.Icons.Images[Vater.getItemIndex(Path.GetExtension(item.SubItems[0].Text))]));
						row.Cells[1].Data = zipfile;
						row.Cells.Add(new XPTable.Models.Cell(item.SubItems[2].Text));
						row.Cells.Add(new XPTable.Models.Cell("Queued"));
						row.Cells.Add(new XPTable.Models.Cell(0));
						row.Cells.Add(new XPTable.Models.Cell(""));
						row.Cells.Add(new XPTable.Models.Cell(""));
						Vater.AddRow(row);
					}
				}
				this.Close();
			}
			else
			{
				MessageBox.Show("File(s) could not be added. Reason: Uploading already in progress");
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
